home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c++
- Subject: Re: Doesn't work, Why EXACTLY?
- Date: Mon, 15 Jan 1996 23:17:51 GMT
- Organization: Netcom
- Message-ID: <30fadc88.273651712@nntp.ix.netcom.com>
- References: <30FA8289.7AD6@bangate.compaq.com>
- NNTP-Posting-Host: ix-dc16-15.ix.netcom.com
- X-NETCOM-Date: Mon Jan 15 3:17:28 PM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- Saurabh Dixit <saurabhd@bangate.compaq.com> wrote:
-
- |>class X {
- |>};
- |>class Y : public X {
- |>};
- |>
- |>class Base {
- |>public:
- |> void func1(int a);
- |> void func2(X* x);
- |>};
- |>
- |>class Derived : public Base {
- |>public:
- |> void func1(float b);
- |> void func2(Y* y);
- |>};
- |>
- |>when I call func2 from a Derived object with a pointer
- |>to an X object, i get - "... cannot convert from X to Y...".
- |>So looks like compiler looks at method func2() in Derived
- |>class. I would think the signature of the call would make
- |>it look in Base class.
- |>On the lines of func1() i thought it would work.
- |>Please note that I don't want func2() to be virtual.
- |>I need func2() to do to distinct operations depending on
- |>what type of object was fed to it?
- |>
- |>Can anyone please explain FULLY?!?!?!
-
- That's the way C++ works. Overloading only takes place within a scope
- and a derived class is a different scope than the base class. From
- the draft (13.2)
-
- Two function declarations of the same name refer to the same
- function
- if they are in the same scope and have equivalent parameter
- declarations. A function member of a derived class is not in
- the
- same scope as a function member of the same name in a base
- class.
-
- [Example:
-
- class B {
- public:
- int f(int);
- };
-
- class D : public B {
- public:
- int f(char*);
- };
-
- Here D::f(char*) hides B::f(int) rather than overloading it.
-
- void h(D* pd)
- {
- pd->f(1); // error:
- // D::f(char*) hides B::f(int)
- pd->B::f(1); // ok
- pd->f("Ben"); // ok, calls D::f
- }
-
- The ARM contains the same language.
-
- If your compiler supports namespaces, you can incorporate the function
- from the base class into the derived class with a using declaration.
-
-
- Michael M Rubenstein
-